package com.qk.controller;

import com.qk.PageResult;
import com.qk.common.Result;
import com.qk.dto.BusinessDto;
import com.qk.entity.Business;
import com.qk.service.BusinessService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

@Slf4j
@RequestMapping("/businesses")
@RestController
public class BusinessController {
    @Autowired
    private BusinessService businessService;

    //2. 添加商机
    @PostMapping
    public Result addBusinesses(@RequestBody Business business){
        log.info("添加商机:{}",business);
        business.setStatus(1);
        business.setCreateTime(LocalDateTime.now());
        business.setUpdateTime(LocalDateTime.now());
        businessService.save(business);
        return Result.success();
    }

    //1. 商机列表查询
    @GetMapping
    public Result getList(BusinessDto business){
        log.info("商机列表请求信息:{}",business);
        PageResult<Business> pageList=businessService.listBusiness(business);
        return Result.success(pageList);
    }

    //3. 分配商机
    @PutMapping("/assign/{businessId}/{userId}")
    public Result updateAssign(@PathVariable Integer businessId,@PathVariable Integer userId){
        log.info("分配商机:{},{}",businessId,userId);
        Business business=new Business();
        business.setId(businessId);
        business.setUserId(userId);
        business.setStatus(2);//跟进中
        business.setUpdateTime(LocalDateTime.now());

        businessService.updateById(business);
        return Result.success();
    }


    //6. 根据ID查询商机
    @GetMapping("/{id}")
    public Result getBusinessById(@PathVariable Integer id){
        log.info("查询商机:{}",id);
        Business business = businessService.getBusinessById(id);
        return Result.success(business);
    }

    //7. 跟进商机
    @PutMapping
    public Result trackBusiness(@RequestBody Business business){
        log.info("跟进商机:{}",business);
        businessService.trackBusiness(business);
        return Result.success();
    }


    //4. 踢回公海
    @PutMapping("/back/{id}")
    public Result toPublicPool(@PathVariable Integer id){
        log.info("踢回公海:{}",id);
        Business businessById = businessService.getById(id);
//        log.info("查询-id={}",businessById);
        businessById.setStatus(4);
        businessById.setUpdateTime(LocalDateTime.now());
        businessService.updateById(businessById);
        return Result.success();
    }

    //5. 转客户处理
    @PostMapping("/toCustomer/{id}")
    public Result toCustomer(@PathVariable Integer id){
        log.info("转客户处理:{}",id);
        businessService.toCustomer(id);
        return Result.success();
    }

    //8. 公海池列表查询
    @GetMapping("/pool")
    public Result getBusinessPools(BusinessDto businessDto){
        log.info("公海池列表查询:{}",businessDto);
        PageResult<Business> pageList = businessService.getBusinessPools(businessDto);
        return Result.success(pageList);
    }
}
